﻿// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX - License - Identifier: Apache - 2.0

namespace HighLevelMappingArbitraryDataExample
{
  using System.Collections.Generic;
  using System.Threading.Tasks;
  using Amazon.DynamoDBv2;
  using Amazon.DynamoDBv2.DataModel;

  // snippet-start:[dynamodb.dotnet35.HighLevelMappingArbitraryData]

  /// <summary>
  /// Shows how to map arbitrary data to an Amazon DynamoDB table. The example
  /// was created using the AWS SDK for .NET version3.7 and .NET Core 5.0.
  /// </summary>
  public class HighLevelMappingArbitraryData
  {
    static async Task Main()
    {
      var client = new AmazonDynamoDBClient();
      DynamoDBContext context = new DynamoDBContext(client);
      await AddRetrieveUpdateBook(context);
    }

    /// <summary>
    /// Creates a book, adds it to the DynamoDB ProductCatalog table, retrieves
    /// the new book from the table, updates the dimensions and writes the
    /// changed item back to the table.
    /// </summary>
    /// <param name="context">The DynamoDB context object used to write and
    /// read data from the table.</param>
    public static async Task AddRetrieveUpdateBook(IDynamoDBContext context)
    {

      // Create a book.
      DimensionType myBookDimensions = new DimensionType()
      {
        Length = 8M,
        Height = 11M,
        Thickness = 0.5M,
      };

      Book myBook = new Book
      {
        Id = 501,
        Title = "AWS SDK for .NET Object Persistence Model Handling Arbitrary Data",
        Isbn = "999-9999999999",
        BookAuthors = new List<string> { "Author 1", "Author 2" },
        Dimensions = myBookDimensions,
      };

      // Add the book to the DynamoDB table ProductCatalog.
      await context.SaveAsync(myBook);

      // Retrieves the book.
      Book bookRetrieved = await context.LoadAsync<Book>(501);

      // Update the book dimensions property.
      bookRetrieved.Dimensions.Height += 1;
      bookRetrieved.Dimensions.Length += 1;
      bookRetrieved.Dimensions.Thickness += 0.2M;

      // Write the changed item to the table.
      await context.SaveAsync(bookRetrieved);
    }
  }

  // snippet-end:[dynamodb.dotnet35.HighLevelMappingArbitraryData]
}
